import sim, sim_items, main
from util import *
import random

class MercurialPrefix(sim_items.Prefix):
    """
    "mercurial" prefix gives affected wearable a random speed bonus. Wearable
    must be armor, and must be footwear.
    """
    def __init__(self):
        super(MercurialPrefix, self).__init__(
            "mercurial", # name
            # description: (gets tacked onto item description)
            "This %(wearable)s has been treated with some chemical smelling "
            "faintly of mink oil and rum, and which, if you concentrate your "
            "senses, appears to impart a fine vibration."
        )

    def initialize(self, wearable):
        """
        This method gets called when the affix is first "applied" to the item
        at creation time -- any one-time changes to the item's properties
        (bonuses, etc.) should take place here; and for more complex affixes,
        this is where you should register to listen for events.
        """
        # check to make sure we want to apply to the given wearable item
        if not isinstance(wearable, sim_items.Armor):
            # the game tried to add us to non-armor. raise an error complaining
            # about it, which if unhandled will cause nyctos to crash. this is
            # because i want the dungeon generation algorithm to be smart enough
            # to only apply scripts in /armor/prefixes (and /armor/suffixes) to
            # armor, and doing otherwise should be considered a bug
            raise ValueError('Tried to apply %r to non-armor item: %r' % (self,
                wearable))

        # on the other hand, if the wearable *is* armor, but not footwear,
        # simply fail to apply *without* raising an error
        # FYI, you can see a list of possible wearSlots in sim_creatures.py
        if 'feet' not in wearable.wearSlots:
            return # just return silently

        # okay let our superclass have a chance to do initialization common to
        # all affixes (this changes the name of the wearable item, modifies its
        # description, etc.). it also sets self.wearable to be the item we're
        # modifying, available for future reference
        super(MercurialPrefix, self).initialize(wearable)

        # now do the stuff particular to this affix. in this case, we just
        # update the item's bonuses

        # pick a random bonus
        self.speedBonus = random.randint(2,6) # random integer b/t 2 and 6
        # add it to any existing speed modifier on the item
        # FYI, you can see a list of possible 'xxxMod' strings in sim_items.py
        self.wearable.bonuses.update({
            'speedMod': self.wearable.bonuses['speedMod'] + self.speedBonus
        })

    def remove(self, wearable):
        """
        This method is the opposite of intialize. It undoes any one-time changes
        we made to the item, and lets us stop listening for events. Currently
        this never gets called, but in the future you can imagine some way for
        items to lose their affixes.
        """
        super(MercurialPrefix, self).remove(wearable)
        self.wearable.bonuses.update({
            'speedMod': self.wearable.bonuses['speedMod'] - self.speedBonus
        })

#========================================
thingClass = MercurialPrefix
